package com.wiscamp.ninechapters.problems.math;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 加减运算等式
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class LatexPlusMinusInequality extends LatexText {
    public LatexPlusMinusInequality(String latexText) {
        this.textValue = latexText;
        this.latexType = LatexTextEnum.PLUS_MINUS_INEQUALITY;
    }

    /**
     * 如果括号外为减法，则反转括号内表达式的运算符
     *
     * @param expression 加减法表达式
     * @return 反转运算符后的表达式
     */
    private static String reverseOperator(String expression) {
        char[] charArray = expression.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (var character : charArray) {
            if (character == '+') {
                character = '-';
            } else if (character == '-') {
                character = '+';
            }
            builder.append(character);
        }
        return builder.toString();
    }

    /**
     * 移除表达式中的括号，暂不考虑括号嵌套
     *
     * @param expression 加减法表达式
     * @param leftBrace  左括号
     * @param rightBrace 右括号
     * @return 移除括号后的表达式
     */
    public String removeBrace(String expression, String leftBrace, String rightBrace) {
        String tempText = expression.replace(" ", "").trim();
        char[] charArray = tempText.toCharArray();
        int left = tempText.indexOf(leftBrace);
        int right = tempText.indexOf(rightBrace);
        if (left == -1 && right == -1) return tempText;
        if (left >= 1) {
            // 左括号不在表达式的第一位
            var operator = charArray[left - 1];
            if (operator == '-') {
                // 左括号前为减号需反转括号内表达式的运算符
                var leftStr = tempText.substring(0, left);
                var middleStr = tempText.substring(left + 1, right);
                var rightStr = tempText.substring(right + 1);
                middleStr = reverseOperator(middleStr);
                tempText = leftStr + middleStr + rightStr;
                return removeBrace(tempText, leftBrace, rightBrace);
            } else {
                var leftStr = tempText.substring(0, left);
                var middleStr = tempText.substring(left + 1, right);
                var rightStr = tempText.substring(right + 1);
                tempText = leftStr + middleStr + rightStr;
                return removeBrace(tempText, leftBrace, rightBrace);
            }
        } else {
            // 左括号在表达式的第一位
            var leftStr = tempText.substring(0, left);
            var middleStr = tempText.substring(left + 1, right);
            var rightStr = tempText.substring(right + 1);
            tempText = leftStr + middleStr + rightStr;
            return removeBrace(tempText, leftBrace, rightBrace);
        }
    }

    /**
     * Get terms
     *
     * @return term list
     */
    @Override
    public Collection<String> toTerms() {
        Collection<String> terms = new ArrayList<>();
        String tempText = textValue;
        if (!tempText.startsWith("+") && !tempText.startsWith("-")) {
            // 表达式的第一个数字补全加号
            tempText = "+" + textValue;
        }

        if (tempText.contains("(")) {
            // 表达式包含括号的情况，移除括号
            tempText = removeBrace(tempText, "(", ")");
        }

        // 将表达式按加减号及数字拆分为term
        // + 6 + 2 - 5 - 2 + 7
        tempText = tempText.replace(" ", "").trim();
        StringBuilder builder = new StringBuilder();
        boolean isFirstTerm = true;
        for (var c : tempText.toCharArray()) {
            if ((c == '+') || (c == '-')) {
                if (isFirstTerm) {
                    builder.append(c);
                    isFirstTerm = false;
                } else {
                    terms.add(builder.toString());
                    builder = new StringBuilder();
                    builder.append(c);
                }
            } else {
                builder.append(c);
            }
        }
        terms.add(builder.toString());
        return terms;
    }

}
